﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using anaii.EasyNews.Data;

namespace anaii.EasyNews
{
    public partial class SettingsList : ModuleSettingsBase
    {
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        public override void UpdateSettings()
        {
            try
            {
                ModuleController controller = new ModuleController();
                int i = 0;
                foreach (KeyValuePair<string, Locale> val in Localization.GetLocales(this.PortalId))
                {
                    TextBox txtNewsListHeaderTemplate = repTemplates.Items[i].FindControl("txtNewsListHeaderTemplate") as TextBox;
                    if (txtNewsListHeaderTemplate != null)
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsListHeaderTemplate" + val.Key, txtNewsListHeaderTemplate.Text);
                    TextBox txtNewsListItemTemplate = repTemplates.Items[i].FindControl("txtNewsListItemTemplate") as TextBox;
                    if (txtNewsListItemTemplate != null)
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsListItemTemplate" + val.Key, txtNewsListItemTemplate.Text);
                    TextBox txtNewsListFooterTemplate = repTemplates.Items[i].FindControl("txtNewsListFooterTemplate") as TextBox;
                    if (txtNewsListFooterTemplate != null)
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsListFooterTemplate" + val.Key, txtNewsListFooterTemplate.Text);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsListSelectedNewsType", ddlNewsType.SelectedValue);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsListSelectedNewsStatus", rblStatus.SelectedValue);
                    controller.UpdateTabModuleSetting(TabModuleId, "NewsListShowPaging", cbShowPaging.Checked.ToString());


                    int tmpInt;
                    if (int.TryParse(tbNumberofNewsPerPage.Text, out tmpInt))
                    {
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsListNumberofNewsPerPage", tmpInt.ToString());
                    }
                    else
                    {
                        controller.UpdateTabModuleSetting(TabModuleId, "NewsListNumberofNewsPerPage", "10");
                    }

                    i++;
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        private void BindData()
        {
            EasyNewsDBDataContext db = new EasyNewsDBDataContext();
            ddlNewsType.Items.Clear();
            ddlNewsType.Items.Add(new ListItem("All", "0"));
            foreach(EasyNewsType ent in  db.EasyNewsTypes)
            {
                ddlNewsType.Items.Add(new ListItem(ent.Name,ent.TypeID.ToString()));
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsListSelectedNewsType"]))
            {
                ddlNewsType.SelectedValue = (string)TabModuleSettings["NewsListSelectedNewsType"];
            }

            bool tmpBool;
            if (TabModuleSettings["NewsListShowPaging"] != null && bool.TryParse(TabModuleSettings["NewsListShowPaging"].ToString(), out tmpBool))
            {
                cbShowPaging.Checked = tmpBool;
            }

            int tmpInt;
            tbNumberofNewsPerPage.Text = "10";
            if (TabModuleSettings["NewsListNumberofNewsPerPage"] != null && int.TryParse(TabModuleSettings["NewsListNumberofNewsPerPage"].ToString(), out tmpInt))
            {
                tbNumberofNewsPerPage.Text = tmpInt.ToString();
            }

            rblStatus.Items.Clear();
            rblStatus.Items.Add(new ListItem("Published", "1"));
            rblStatus.Items.Add(new ListItem("Archive", "2"));
            rblStatus.SelectedIndex = 0;
            if (!string.IsNullOrEmpty((string)TabModuleSettings["NewsListSelectedNewsStatus"]))
            {
                rblStatus.SelectedValue = (string)TabModuleSettings["NewsListSelectedNewsStatus"];
            }

            repTemplates.DataSource = Localization.GetLocales(this.PortalId);
            repTemplates.DataBind();
        }

        protected void repTemplates_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                KeyValuePair<string, Locale> valueData = (KeyValuePair<string, Locale>)e.Item.DataItem;

                TextBox txtNewsListHeaderTemplate = e.Item.FindControl("txtNewsListHeaderTemplate") as TextBox;
                if (txtNewsListHeaderTemplate != null)
                {
                    if ((string)TabModuleSettings["NewsListHeaderTemplate" + valueData.Key] != string.Empty)
                    {
                        txtNewsListHeaderTemplate.Text = (string)TabModuleSettings["NewsListHeaderTemplate" + valueData.Key];
                    }
                }
                TextBox txtNewsListItemTemplate = e.Item.FindControl("txtNewsListItemTemplate") as TextBox;
                if (txtNewsListItemTemplate != null)
                {
                    if ((string)TabModuleSettings["NewsListItemTemplate" + valueData.Key] != string.Empty)
                    {
                        txtNewsListItemTemplate.Text = (string)TabModuleSettings["NewsListItemTemplate" + valueData.Key];
                    }
                }
                TextBox txtNewsListFooterTemplate = e.Item.FindControl("txtNewsListFooterTemplate") as TextBox;
                if (txtNewsListFooterTemplate != null)
                {
                    if ((string)TabModuleSettings["NewsListFooterTemplate" + valueData.Key] != string.Empty)
                    {
                        txtNewsListFooterTemplate.Text = (string)TabModuleSettings["NewsListFooterTemplate" + valueData.Key];
                    }
                }
            }
        }
    }
}